package siebje.mobi.android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.CheckBox;
import android.widget.TextView;

public class MahjonggSettings extends Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
{
    List<RadioButton> radiobuttons = new ArrayList<RadioButton>();
    List<TextView> textviews = new ArrayList<TextView>();
    List<Integer> specialScores = new ArrayList<Integer>();
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        ((Button)findViewById(R.id.SettingsDone)).setOnClickListener(this);
        ((Button)findViewById(R.id.SettingsDoneTop)).setOnClickListener(this);
        //put all references into nice arraylists
        getAllViews();
        
        Bundle extras = getIntent().getExtras();
        if(extras != null){
        	int specialIndex = extras.getInt("specialIndex");
        	radiobuttons.get(specialIndex).setChecked(true);
        	
        	boolean drewFromWall = extras.getBoolean("drewFromWall");
			((CheckBox)findViewById(R.id.DrewFromWall))
					.setChecked(drewFromWall);
			
			boolean onlyTile = extras.getBoolean("onlyTile");
			((CheckBox)findViewById(R.id.OnlyTile))
					.setChecked(onlyTile);
			
			boolean lastBeforeDead = extras.getBoolean("lastBeforeDead");
			((CheckBox)findViewById(R.id.LastBeforeDead))
					.setChecked(lastBeforeDead);
			
			boolean finishedLoose = extras.getBoolean("finishedLoose");
			((CheckBox)findViewById(R.id.FinishedLoose))
					.setChecked(finishedLoose);
			
			boolean allButLastConcealed = 
				extras.getBoolean("allButLastConcealed");
			((CheckBox)findViewById(R.id.AllButLastConcealed))
					.setChecked(allButLastConcealed);
			
			boolean robbingKong = extras.getBoolean("robbingKong");
			((CheckBox)findViewById(R.id.RobbingKong))
					.setChecked(robbingKong);
        }
    }
    
    //make sure we return properly
    @Override
    public void onBackPressed()
    {
    	onClick(findViewById(R.id.SettingsDone));
    }

	public void onClick(View v) {
		if(v.equals(findViewById(R.id.SettingsDone)) ||
				v.equals(findViewById(R.id.SettingsDoneTop)))
		{
			Intent result = new Intent();
			int additionalScore = 0;
			int additionalMult = 1;
			
			//Handle additional settings
			boolean drewFromWall = 
				((CheckBox)findViewById(R.id.DrewFromWall)).isChecked();
			if(drewFromWall)
				additionalScore += 2;
			result.putExtra("drewFromWall", drewFromWall);
			
			boolean onlyTile = 
				((CheckBox)findViewById(R.id.OnlyTile)).isChecked();
			if(onlyTile)
				additionalScore += 10;
			result.putExtra("onlyTile", onlyTile);
			
			boolean lastBeforeDead =
				((CheckBox)findViewById(R.id.LastBeforeDead)).isChecked();
			if(lastBeforeDead)
				additionalScore += 10;
			result.putExtra("lastBeforeDead", lastBeforeDead);
			
			boolean finishedLoose = 
				((CheckBox)findViewById(R.id.FinishedLoose)).isChecked();
			if(finishedLoose)
				additionalScore += 10;
			result.putExtra("finishedLoose", finishedLoose);
			
			boolean allButLastConcealed = 
				((CheckBox)findViewById(R.id.AllButLastConcealed)).isChecked();
			if(allButLastConcealed)
				{} //nop
			result.putExtra("allButLastConcealed", allButLastConcealed);
			
			boolean robbingKong = 
				((CheckBox)findViewById(R.id.RobbingKong)).isChecked();
			if(robbingKong)
				additionalScore += 10;
			result.putExtra("robbingKong", robbingKong);
			
			//Handle Special Games
			for(int k = 0 ; k < radiobuttons.size() ; k++) {
				if(radiobuttons.get(k).isChecked()) {
					result.putExtra("special", specialScores.get(k));
					result.putExtra("specialText", 
							textviews.get(k).getText());
					result.putExtra("specialTitle", 
							radiobuttons.get(k).getText());
					result.putExtra("specialIndex", k);
				}
			}
			
			result.putExtra("score", additionalScore);
			result.putExtra("multiplier", additionalMult);
			
			setResult(ScoreTabs.SETTINGS_CALL, result);
			finish();
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked)
		{
			for(RadioButton r : radiobuttons)
			{
				int index = radiobuttons.indexOf(r);
				if(!(buttonView == r))
				{
					r.setChecked(false);
					textviews.get(index).setVisibility(View.GONE);
				}
				else
				{
					textviews.get(index).setVisibility(View.VISIBLE);
				}
			}
		}
		
	}
	
	public void getAllViews()
	{
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton01));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton02));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton03));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton04));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton05));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton06));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton07));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton08));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton09));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton10));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton11));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton12));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton13));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton14));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton15));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton16));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton17));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton18));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton19));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton20));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton21));
		radiobuttons.add((RadioButton)findViewById(R.id.RadioButton22));
		
		textviews.add((TextView)findViewById(R.id.SpecialTV01));
		textviews.add((TextView)findViewById(R.id.SpecialTV02));
		textviews.add((TextView)findViewById(R.id.SpecialTV03));
		textviews.add((TextView)findViewById(R.id.SpecialTV04));
		textviews.add((TextView)findViewById(R.id.SpecialTV05));
		textviews.add((TextView)findViewById(R.id.SpecialTV06));
		textviews.add((TextView)findViewById(R.id.SpecialTV07));
		textviews.add((TextView)findViewById(R.id.SpecialTV08));
		textviews.add((TextView)findViewById(R.id.SpecialTV09));
		textviews.add((TextView)findViewById(R.id.SpecialTV10));
		textviews.add((TextView)findViewById(R.id.SpecialTV11));
		textviews.add((TextView)findViewById(R.id.SpecialTV12));
		textviews.add((TextView)findViewById(R.id.SpecialTV13));
		textviews.add((TextView)findViewById(R.id.SpecialTV14));
		textviews.add((TextView)findViewById(R.id.SpecialTV15));
		textviews.add((TextView)findViewById(R.id.SpecialTV16));
		textviews.add((TextView)findViewById(R.id.SpecialTV17));
		textviews.add((TextView)findViewById(R.id.SpecialTV18));
		textviews.add((TextView)findViewById(R.id.SpecialTV19));
		textviews.add((TextView)findViewById(R.id.SpecialTV20));
		textviews.add((TextView)findViewById(R.id.SpecialTV21));
		textviews.add((TextView)findViewById(R.id.SpecialTV22));
		
		specialScores.add(0);
		specialScores.add(3000);
		specialScores.add(3000);
		specialScores.add(2000);
		specialScores.add(3000);
		specialScores.add(3000);
		specialScores.add(2000);
		specialScores.add(3000);
		specialScores.add(3000);
		specialScores.add(2000);
		specialScores.add(1000);
		specialScores.add(1000);
		specialScores.add(3000);
		specialScores.add(1000);
		specialScores.add(1000);
		specialScores.add(3000);
		specialScores.add(2000);
		specialScores.add(3000);
		specialScores.add(2000);
		specialScores.add(3000);
		specialScores.add(3000);
		specialScores.add(3000);
		
		for(RadioButton r : radiobuttons)
		{
			r.setOnCheckedChangeListener(this);
		}
	}
}
